﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ProductImageController
/// </summary>
public class ProductImageController:Interface
{
    private SqlConnection con;
	public ProductImageController()
	{
	    con = ConnectDB.Connect();
        ConnectDB.Open();
	}

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_Product_Images";
            cmd.CommandType = CommandType.StoredProcedure;
            ProductImage pi = (ProductImage)obj;
            cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = pi.Product_id;
            cmd.Parameters.Add("@image", SqlDbType.NText).Value = pi.Image;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = pi.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = pi.Status;
            cmd.Parameters.Add("@dateStart", SqlDbType.DateTime).Value = pi.DateStart;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Update(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update_Product_Images";
            cmd.CommandType = CommandType.StoredProcedure;
            ProductImage pi = (ProductImage)obj;
            cmd.Parameters.Add("@product_image_id", SqlDbType.Int).Value = pi.ProductImage_id;
            cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = pi.Product_id;
            cmd.Parameters.Add("@image", SqlDbType.NText).Value = pi.Image;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = pi.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = pi.Status;
            cmd.Parameters.Add("@dateUpdate", SqlDbType.DateTime).Value = pi.DateUpdate;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete_Product_Images";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@product_image_id", SqlDbType.Int).Value = id;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public DataTable GetAll()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product_Images ORDER BY dateStart DESC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Product_Images WHERE product_image_id=@product_image_id ORDER BY dateStart DESC";
            cmd.Parameters.AddWithValue("@product_image_id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public DataTable GetByProductID(int product_id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Product_Image WHERE product_id=@product_id";
            cmd.Parameters.AddWithValue("@product_id", product_id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public int deleteImageProduct(int product_image_id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "DELETE FROM Product_Image WHERE product_image_id=@product_image_id";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@product_image_id", SqlDbType.Int).Value = product_image_id;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }
}